package com.lex.notepad;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;

import android.database.ContentObservable;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class NotePadMainActivity extends Activity{

    private String saveName,savePassword;

    private Button login,register_new_user;
    private EditText field_name;//接收用户名
    private EditText field_pass;//接收密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_pad_main);
        getContentResolver().registerContentObserver(Uri.parse("content://com.lex.db/person"), true,new MyContentResolver());
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", "hahahaahahaha");
        getContentResolver().insert(Uri.parse("content://com.lex.db/person"), contentValues);
        findView();
        initView();
    }

    class MyContentResolver extends ContentObserver {

        public MyContentResolver() {
            super(new Handler());
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Log.d("lex","数据发生改变--------------------------");
        }
    }

    protected void findView(){
        field_name = (EditText) findViewById(R.id.user_name);  //首先获取用来输入用户名的组件
        field_pass = (EditText) findViewById(R.id.pass_words); //同时也需要获取输入密码的组件
        login = (Button) findViewById(R.id.login);
        register_new_user = (Button) findViewById(R.id.register_new_user);
    }

    protected void initView(){
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mainName = field_name.getText().toString();
                String mainPassword = field_pass.getText().toString();
                if (mainName.length() <= 0 && mainPassword.length() <= 0) {
                    Toast.makeText(NotePadMainActivity.this, "用户名或密码为空", Toast.LENGTH_SHORT).show();
                } else if (mainName.length()<=0) {
                    Toast.makeText(NotePadMainActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                } else if (mainPassword.length()<=0) {
                    Toast.makeText(NotePadMainActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                } else if (mainName != null && mainPassword != null) {
                    // 获取存储的数据
                    SharedPreferences sp = getSharedPreferences(mainName, MODE_PRIVATE);
                    saveName = sp.getString( "name", "null");
                    savePassword = sp.getString("password","null");
                    //判断用户名与密码是否和保存的数据一致，进行提醒或者登录
                    if (saveName.equals(mainName) && savePassword.equals(mainPassword)) {
                        //实现界面的跳转
                        Intent intent = new Intent(NotePadMainActivity.this, Notepad_pagesActivity.class);
                            intent.putExtra("new",savePassword);
                            startActivity(intent);
                        //关闭当前界面
                        finish();
                    }else{
                        Toast.makeText(NotePadMainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        register_new_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(NotePadMainActivity.this,RegisterActivity.class));
                finish();
            }
        });

    }
}
